package com.contactmanager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class EditGroups extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Restore preferences
		SharedPreferences settings = getSharedPreferences("preferences", 0);
		int theme = settings.getInt("curTheme", R.style.AppBaseTheme);
		System.out.println(theme);
		setTheme(theme);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_groups);
		
		//Get all groups
		new GetAllGroupsTask().execute();
		
		//Navigates back to AllContacts
		Button cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v){
				Intent myIntent = new Intent(EditGroups.this, AllContacts.class);
				startActivityForResult(myIntent, 0);
			}
		});
		
		//TODO edit database entry for each group name
		Button saveButton = (Button) findViewById(R.id.saveButton);
		saveButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v){
				new PutAllGroupsTask().execute();
				Intent myIntent2 = new Intent(EditGroups.this, AllContacts.class);
				startActivityForResult(myIntent2, 0);
			}
		});
		
	}
	
	private class GetAllGroupsTask extends AsyncTask<Void, Integer, ArrayList<Group>>{

		@Override
		protected ArrayList<Group> doInBackground(Void... nothing) {
			
			ContactsDataSource cds2 = new ContactsDataSource(EditGroups.this);
			
			cds2.open();
			
			ArrayList<Group> groups = cds2.getAllGroups();
			
			cds2.close();
			
			return groups;
		}
		
        @Override
        protected void onPostExecute(ArrayList<Group> groupList) {
        	EditText editGroup1 = (EditText) findViewById(R.id.editGroup1);
        	EditText editGroup2 = (EditText) findViewById(R.id.editGroup2);
        	EditText editGroup3 = (EditText) findViewById(R.id.editGroup3);
        	EditText editGroup4 = (EditText) findViewById(R.id.editGroup4);
        	EditText editGroup5 = (EditText) findViewById(R.id.editGroup5);
        	editGroup1.setText(groupList.get(1).getGroupName());
    		editGroup2.setText(groupList.get(2).getGroupName());
    		editGroup3.setText(groupList.get(3).getGroupName());
    		editGroup4.setText(groupList.get(4).getGroupName());
    		editGroup5.setText(groupList.get(5).getGroupName());
        }
        
	}
        
    	private class PutAllGroupsTask extends AsyncTask<Void, Integer, Void>{

    		@Override
    		protected Void doInBackground(Void... nothing) {
    			
    			ContactsDataSource cds2 = new ContactsDataSource(EditGroups.this);
    			
    			cds2.open();
    			
            	EditText editGroup1 = (EditText) findViewById(R.id.editGroup1);
            	EditText editGroup2 = (EditText) findViewById(R.id.editGroup2);
            	EditText editGroup3 = (EditText) findViewById(R.id.editGroup3);
            	EditText editGroup4 = (EditText) findViewById(R.id.editGroup4);
            	EditText editGroup5 = (EditText) findViewById(R.id.editGroup5);
            	Group g1 = new Group(2,editGroup1.getText().toString());
            	Group g2 = new Group(3,editGroup2.getText().toString());
            	Group g3 = new Group(4,editGroup3.getText().toString());
            	Group g4 = new Group(5,editGroup4.getText().toString());
            	Group g5 = new Group(6,editGroup5.getText().toString());
            	
            	try {
					cds2.updateGroups(g1);
	            	cds2.updateGroups(g2);
	            	cds2.updateGroups(g3);
	            	cds2.updateGroups(g4);
	            	cds2.updateGroups(g5);
				} catch (Exception e) {
					e.printStackTrace();
				}
    			updateTime();
    			cds2.close();
				return null;
    		}
    		
	}
    	
    	public void updateTime() {
    		SharedPreferences settings = getSharedPreferences("preferences", 0);
    		SharedPreferences.Editor editor = settings.edit();
    		Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long sysTime = -1;
    		String myDate = new String(sdf.format(cal.getTime()));
    		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		Date date;
    		try {
    			date = format.parse(myDate);
    			sysTime=date.getTime();
    		} catch (java.text.ParseException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		editor.putLong("lastUpdate",sysTime);
    		// Commit the edits!
    		editor.commit();
    	}
}
	
